var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    searchName:"",
    is_authorized_geography:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.getSetting({
      success:(res)=> {
        console.log(res.authSetting['scope.userLocation']);
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success: (res)=> {
              wx.getLocation({
                type: 'wgs84',
                success:(res)=> {
                  console.log("定位位置：", res);
                  wx.setStorageSync("longitude", res.longitude);
                  wx.setStorageSync("latitude", res.latitude);
                  this.clientPublic_location();
                }, fail() {
                    console.log(res)
                }
              })
            }, fail() {

            }
          })
        } else {
          wx.getLocation({
            type: 'wgs84',
            success: (res)=> {
              console.log(1283);
              wx.setStorageSync("longitude", res.longitude);
              wx.setStorageSync("latitude", res.latitude);
              this.clientPublic_location();
            }, fail(r) {

            }
          })
        }
      }
    })

  },

  gotoSetting() {
    wx.openSetting({
      success: (res) => {
        console.log(res)
      }
    })
  },

  clientPublic_location: function () {
    var url = app.globalData.domain + "/clientPublic/location";
    var parm = {
      longitude: wx.getStorageSync("longitude"),
      latitude: wx.getStorageSync("latitude"),
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("小区数据", res.data);
      this.setData({
        list: res.data
      });

    }, error => {
      Common.showToast(error);
    });
  },

  searchByName:function(){
    var _that = this;
    if (app.checkNull(_that.data.searchName)) {
      Common.showToast("请填写搜索小区的名称");
      return false;
    }
    var url = app.globalData.domain + "/client/community/searchByName";
    var parm = {
      name: this.data.searchName,
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("搜索小区数据", res.data);
      _that.setData({
        list: res.data
      });

    }, error => {
      Common.showToast(error);
    });
  },
  navigatorTo: function (event){
    console.log(event)
    wx.setStorageSync("idCommunity", event.currentTarget.dataset.id);
    wx.setStorageSync("nameCommunity", event.currentTarget.dataset.name);
    wx.navigateBack()
  },
  saveSearchByName: function (event) {
    console.log("搜索名", event);
    
    this.setData({
      searchName: event.detail.value
    });



  },
  search: function (event) {
    console.log("搜索名", event.detail.value);

    this.setData({
      searchName: event.detail.value
    });

    var url = app.globalData.domain + "/client/community/searchByName";
    var parm = {
      name: this.data.searchName,
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("搜索小区数据", res.data);
      this.setData({
        list: res.data
      });

    }, error => {
      Common.showToast(error);
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(1111);

    if ( wx.getStorageSync("longitude") != "" ){
      this.setData({
        is_authorized_geography:1
      })
    }

    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        wx.setStorageSync("longitude", res.longitude);
        wx.setStorageSync("latitude", res.latitude);
        this.setData({
          is_authorized_geography: 1
        })
        this.clientPublic_location();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



})